package com.soft.springhrms.controller;

import com.soft.springhrms.mapper.ApprovesalaryMapper;
import com.soft.springhrms.pojo.Approvesalary;
import com.soft.springhrms.pojo.Performances;
import com.soft.springhrms.pojo.Requests;
import com.soft.springhrms.pojo.Salaries;
import com.soft.springhrms.service.*;
import com.soft.springhrms.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/HR")
public class HRController {
    @Autowired
    private RequestsService requestsService;
    @Autowired
    private SalariesService salariesService;
    @Autowired
    private PerformancesService performancesService;
    @Autowired
    private EmployeesService employeeService;
    @Autowired
    private ApprovesalaryService approvesalaryService;
    @Autowired
    private AttendanceService attendanceService;

    // 人事获取自己申请的工资审批的历史记录
    @GetMapping("/getApproveSalary")
    public ResultVO getApproveSalary(Integer employeeId,@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
        return requestsService.querySalaryRequests(employeeId, pageNum, pageSize);
    }

    // 人事查看员工的请假/加班请求
    @GetMapping("/showRequests")
    public ResultVO showRequestAdmin(@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
        return requestsService.queryRequestsHR(pageNum, pageSize);
    }

    // 人事审批员工的请假/加班请求
    @PostMapping("/approve")
    public ResultVO approveHRRequest(@RequestBody Requests requests) {
        return requestsService.approveRequestsHR(requests);
    }

    // 人事查看自己的工资发放历史
    @GetMapping("/getSalaries")
    public ResultVO getSalaries(@RequestParam Integer employeeId, @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "size",defaultValue = "10") int size){
        return salariesService.querySalariesById(employeeId, page, size);
    }

    // 人事查看所有员工的绩效
    @GetMapping("/getAllPerformance")
    public ResultVO getAllPerformance(@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize){
        return performancesService.queryAllPerformances(pageNum, pageSize);
    }

    // 人事修改员工的绩效
    @PostMapping("/updatePerformance")
    public ResultVO updatePerformance(@RequestBody Performances performance){
        return performancesService.updatePerformances(performance);
    }

    // 人事修改员工的工资
    @PostMapping("/updateSalary")
    public ResultVO updateSalary(@RequestBody Salaries salaries){
        return salariesService.updateSalaries(salaries);
    }

    // 给人事在绩效页面展示工资
    @GetMapping("/getSalaryById")
    public ResultVO getSalary(@RequestParam Integer employeeId){
        return employeeService.getApproveSalaryById(employeeId);
    }

    // 人事申请发放工资
    @PostMapping("/addApproveSalary")
    public ResultVO addApproveSalary(@RequestBody Approvesalary approvesalary){
        return approvesalaryService.addApproveSalary(approvesalary);
    }
    @GetMapping("/changeEmployeeAttendanceStatus/{id}/{Status}")
    public ResultVO changeEmployeeAttendanceStatus(@PathVariable Integer id,@PathVariable Integer Status,@RequestParam String clockTime){
        String attendanceStatus;
        switch (Status){
            case 2:attendanceStatus="出勤";
                break;
            case 3:attendanceStatus="请假";
                break;
            default:attendanceStatus="缺勤";
                break;
        }
        try{
            Integer row= attendanceService.changeEmployeeAttendanceStatus(clockTime,id,attendanceStatus);
            if(row>=1){
            return ResultVO.success("","更改成功");}else{
                return ResultVO.error(401,"修改失败");}
        }catch (Exception e){
            e.printStackTrace();
            return ResultVO.error(405,"请求参数错误");}
    }

    // 人事查看员工的缺勤次数
    @GetMapping("/getAbsenceCount")
    public ResultVO getAbsenceCount(@RequestParam Integer employeeId) {
        return attendanceService.getAbsenceCount(employeeId);
    }

    // 人事给所有员工发放默认绩效记录
    @PostMapping("/addPerformanceAll")
    public ResultVO addPerformanceAll() {
        return performancesService.addPerformancesAll();
    }

    // 人事给指定员工发放默认绩效记录
    @GetMapping("/addPerformanceById")
    public ResultVO addPerformanceById(@RequestParam Integer employeeId) {
        return performancesService.addPerformancesById(employeeId);
    }
}
